#include "widget.h"
#include "ui_widget.h"
#include <time.h>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //设置进度条颜色
    ui->progressBar->setStyleSheet("QProgressBar::chunk { background-color: red; }");
    //设置百分比数字的位置
    ui->progressBar->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    //创建定时器对象
    timer = new QTimer(this);
    //连接信号槽
    connect(timer, &QTimer::timeout, this, &Widget::handler);
    //启动定时器
    timer->start(300);
    //设置随机数种子
    srand(time(nullptr));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::handler()
{
    //获取进度条的当前数值
    int value = ui->progressBar->value();
    if(value >= 100)
    {
        timer->stop();
        return;
    }
    //获取增加的随机数值
    int num = rand() % 5 + 1;
    ui->progressBar->setValue(value + num);
}

